// pages/logs/logs.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    outQuestion: [],
    getQuestion: [],
    hasOut: false,
    hasGet: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          currentPage: options.id,
        })
      }
    })

    getlogs(this, options.id)
  },
  switchNav: function (e) {
    if (e.target.id != this.data.currentPage) {
      if (e.target.id == 1) {
        if (!this.data.hasOut) {
          getlogs(this, 1)
        } else {
          this.setData({
            Questions: this.data.outQuestion
          })
        }
      } else if (e.target.id == 2) {
        if (!this.data.hasGet) {
          getlogs(this, 2)
        } else {
          this.setData({
            Questions: this.data.getQuestion
          })
        }
      }

    }
    this.setData({
      currentPage: e.target.id
    })
  },
  toMyquestion: function (e) {
    if (e.target.dataset.qtype == 1) {
      wx.navigateTo({
        url: '../../pages/myquestions/myquestions?question_id=' + e.target.id,
      })
    } else {
      wx.navigateTo({
        url: '../../pages/mySinglequestions/mySinglequestions?question_id=' + e.target.id,
      })
    }

  },
  toResult: function (e) {
    if (e.target.dataset.qtype == 1) {
      wx.navigateTo({
        url: '../../pages/result/result?id=2&question_id=' + e.target.id,
      })
    } else {
      wx.navigateTo({
        url: '../../pages/singleResult/singleResult?id=2&question_id=' + e.target.id,
      })
    }
  }
})

function getlogs(that, _id) {
  wx.request({
    url: app.globalData.allUrl + "/api/my_question.php",
    data: {
      user_id: wx.getStorageSync('userid'),
      type_id: _id
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      console.log(res.data)
      if (_id == 1) {
        var _allLogs = [];
      if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            _allLogs.push(res.data.data[i])
          }
        }
        if (res.data.list) {
          for (var j = 0; j < res.data.list.length; j++) {
            _allLogs.push(res.data.list[j])
          }
        }
        //  console.log(_allLogs)
        that.setData({
          outQuestion: _allLogs,
          Questions: _allLogs,
          hasOut: true,
        })
      } else if (_id == 2) {
        var _allLogs2 = [];
       if (res.data.data) {
          for (var a = 0; a < res.data.data.length; a++) {
            _allLogs2.push(res.data.data[a])
          }
        }
        if (res.data.list) {
          for (var b = 0; b < res.data.list.length; b++) {
            _allLogs2.push(res.data.list[b])
          }
        }
        that.setData({
          getQuestion: _allLogs2,
          Questions: _allLogs2,
          hasGet: true,
        })
      }
    }, fail: function () {
    }
  })
}